import {createRouter, createWebHistory,createWebHashHistory} from 'vue-router'
import LoginView from "@/views/LoginView";
import Layout from "@/layout/AdminLayout";
import PersonView from "@/views/PersonView";
import StuMgt from "@/views/StuMgt";
import WelcomeAdmin from "@/views/WelcomeAdmin";
import TeaMgt from "@/views/TeaMgt";
import AdminInfo from "@/views/AdminInfo";
import WelcomeUser from "@/views/WelcomeTea";
import ResetPassword from "@/views/ResetPassword";
import StuLayout from "@/layout/StuLayout";
import TeaLayout from "@/layout/TeaLayout";
import WelcomeStu from "@/views/WelcomeStu";
import WelcomeTea from "@/views/WelcomeTea";
import CollegeSchool from "@/views/CollegeSchool";
import courseInfo from "@/views/CourseInfo";

import CourseInfo from "@/views/CourseInfo";
import CourseAudit from "@/views/CourseAudit";
import EAArrangement from "@/views/EAArrangement";
import CourseArrangement from "@/views/CourseArrangement";
import TeaInfo from "@/views/TeaInfo";
import StuInfo from "@/views/StuInfo";
import CourseMaint from "@/views/CourseMaint";
import CourseSelect from "@/views/CourseSelect";
import CourseRoll from "@/views/CourseRoll";
import RollAudit from "@/views/RollAudit";
import CourseApply from "@/views/CourseApply";
import CourseFinish from "@/views/CourseFinish";
import CourseTeach from "@/views/CourseTeach";
import TimePlace from "@/views/TimePlace";
import TimeArrangement from "@/views/TimeArrangement";
import placeArrangement from "@/views/PlaceArrangement";
import CourseOption from "@/views/CourseOption";
import coursePast from "@/views/coursePast";

const routes = [
    {
        path: '/',
        name: 'Layout',
        redirect: "/login",
        component: Layout,
        children: [
            {
                path: '/welAdmin',
                name: 'WelAdmin',
                component: WelcomeAdmin,
                // meta: {requireAuth: true}
            },
            {
                path: '/stuMgt',
                name: 'StuMgt',
                component: StuMgt,
                // meta: {requireAuth: true}
            },
            {
                path: '/teaMgt',
                name: 'TeaMgt',
                component: TeaMgt,
                // meta: {requireAuth: true}
            },
            {
                path: '/adminInfo',
                name: 'AdminInfo',
                component: AdminInfo,
                //meta: {requireAuth: true}
            },
            {
                path: '/person',
                name: 'Person',
                component: PersonView,
                //meta: {requireAuth: true}
            },
            {
                path: '/welAdmin',
                name: 'WelAdmin',
                component: WelcomeAdmin,
                //meta: {requireAuth: true}
            },
            {
                path: '/collegeSchool',
                name: 'CollegeSchool',
                component: CollegeSchool,
                //meta: {requireAuth: true}
            },
            {
                path: '/courseInfo',
                name: 'CourseInfo',
                component: CourseInfo,
                //meta: {requireAuth: true}
            },
            {
                path: '/courseAudit',
                name: 'CourseAudit',
                component: CourseAudit,
                //meta: {requireAuth: true}
            },
            {
                path: '/eaa',
                name: 'EAA',
                component: EAArrangement,
                //meta: {requireAuth: true}
            },
            {
                path: '/timeArrangement',
                name: 'TimeArrangement',
                component: TimeArrangement,
                //meta: {requireAuth: true}
            },
            {
                path: '/placeArrangement',
                name: 'PlaceArrangement',
                component: placeArrangement,
                //meta: {requireAuth: true}
            },
            {
                path: '/courseArrangement',
                name: 'CourseArrangement',
                component: CourseArrangement,
                //meta: {requireAuth: true}
            },
            {
                path: '/courseRoll',
                name: 'CourseRoll',
                component: CourseRoll,
                //meta: {requireAuth: true}
            },
            {
                path: '/rollAudit',
                name: 'RollAudit',
                component: RollAudit,
                //meta: {requireAuth: true}
            },
            {
                path: '/coursePast',
                name: 'coursePast',
                component: coursePast,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
        ]
    },
    {
        path: '/',
        name: '/StuLayout',
        component: StuLayout,
        children: [
            {
                path: '/welStu',
                name: 'WelStu',
                component: WelcomeStu,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/stuInfo',
                name: 'StuInfo',
                component: StuInfo,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseSelect',
                name: 'CourseSelect',
                component: CourseSelect,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseApply',
                name: 'CourseApply',
                component: CourseApply,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseFinish',
                name: 'CourseFinish',
                component: CourseFinish,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseOption',
                name: 'CourseOption',
                component: CourseOption,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },

        ]
    },
    {
        path: '/',
        name: '/TeaLayout',
        component: TeaLayout,
        children: [
            {
                path: '/welTea',
                name: 'WelTea',
                component: WelcomeTea,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/teaInfo',
                name: 'TeaInfo',
                component: TeaInfo,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseMaint',
                name: 'CourseMaint',
                component: CourseMaint,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
            {
                path: '/courseTeach',
                name: 'CourseTeach',
                component: CourseTeach,
                // meta: {requireAuth: true}  // 添加该字段，表示进入这个路由是需要登录的},
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },
    {
        path: '/resetPassword',
        name: 'ResetPassword',
        component: ResetPassword,
        //meta: {requireAuth: true}
    },
    {
        path: '/timePlace',
        name: 'TimePlace',
        component: TimePlace,
        //meta: {requireAuth: true}
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    const type = localStorage.getItem("type")
    const status = localStorage.getItem("status")
    if (to.matched.some(record => record.meta.requireAuth)) {  // 判断该路由是否需要登录权限
        if (localStorage.getItem("token") && type === '3') {  // 判断当前的token是否存在
            next();
        } else {

            if ((type === '2' || type === '1') && (to.fullPath === '/welUser' || to.fullPath === '/resetPassword') && status) {
                next()
            } else {
                next({
                    path: '/login',
                    query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }

        }
    } else {
        next();
    }
});

export default router
